package com.coolbi.onsupai.core.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.CommonUtil;
import com.coolbi.onsupai.core.biz.GoodsInfoBiz;
import com.coolbi.onsupai.core.constant.GoodsConstant;
import com.coolbi.onsupai.core.dao.GoodsInfoDao;
import com.coolbi.onsupai.core.entity.GoodsInfo;
import com.coolbi.onsupai.core.exception.ServiceException;

public class GoodsInfoBizImpl implements GoodsInfoBiz {
	
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	
	public void setGoodsInfoDao(GoodsInfoDao goodsInfoDao) {
		this.goodsInfoDao = goodsInfoDao;
	}

	@Override
	public GoodsInfo save(GoodsInfo obj) {

		try {
			goodsInfoDao.save(obj);
		} catch (Exception e) {
			throw new ServiceException("添加商品信息发生错误！", e);
		}
		return obj;
	}

	@Override
	public void delete(long id) {
		try {
			goodsInfoDao.delete(id);
		} catch (Exception e) {
			throw new ServiceException("删除商品信息发生错误！", e);
		}
	}

	@Override
	public void update(GoodsInfo obj) {
		try {
			goodsInfoDao.update(obj);
		} catch (Exception e) {
			throw new ServiceException("更新商品信息发生错误！", e);
		}
	}

	@Override
	public GoodsInfo get(long id) {
		return goodsInfoDao.get(id);
	}

	@Override
	public List<GoodsInfo> list() {
		return goodsInfoDao.list();
	}

	@Override
	public List<GoodsInfo> listByPage(int offset, int limit) {
		
		return goodsInfoDao.listByPage(offset, limit);
	}

	@Override
	public List<GoodsInfo> find(Map<String, Object> parameter) {
		return goodsInfoDao.find(parameter);
	}

	@Override
	public List<GoodsInfo> findByPage(Map<String, Object> parameter,
			int offset, int limit) {
		return goodsInfoDao.findByPage(parameter, offset, limit);
	}

	@Override
	public List<GoodsInfo> search(Map<String, Object> parameter, int offset,
			int limit) {
		return goodsInfoDao.search(parameter, offset, limit);
	}

	@Override
	public int count() {
		return goodsInfoDao.count();
	}

	@Override
	public int countByKeyWord(Map<String, Object> parameter) {
		return goodsInfoDao.countByKeyWord(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return goodsInfoDao.count(parameter);
	}

	@Override
	public List<GoodsInfo> queryByKeyWord(int searchType, String keyWord,
			int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		switch (searchType) {
		case GoodsConstant.GOODS_GOODS_NO:
			parameter.put("goodsNo", keyWord);
			break;
		case GoodsConstant.GOODS_GOODS_NAME:
			parameter.put("goodsName", keyWord);
			break;
		case GoodsConstant.GOODS_ENABLE_STOCK:
			parameter.put("enableStock", keyWord);
			break;
		case GoodsConstant.GOODS_SALE_STATUS:
			parameter.put("saleStatus", keyWord);
			break;
		case GoodsConstant.GOODS_CATEGORY:
			parameter.put("goodsCategoryId", keyWord);
			break;
		default:
			break;
		}
		return goodsInfoDao.search(parameter, offset, limit);
	}

	@Override
	public int countByKeyWord(int searchType, String keyWord) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		switch (searchType) {
		case GoodsConstant.GOODS_GOODS_NO:
			parameter.put("goodsNo", keyWord);
			break;
		case GoodsConstant.GOODS_GOODS_NAME:
			parameter.put("goodsName", keyWord);
			break;
		case GoodsConstant.GOODS_ENABLE_STOCK:
			parameter.put("enableStock", keyWord);
			break;
		case GoodsConstant.GOODS_SALE_STATUS:
			parameter.put("saleStatus", keyWord);
			break;
		case GoodsConstant.GOODS_CATEGORY:
			parameter.put("goodsCategoryId", keyWord);
			break;
		default:
			break;
		}
		return goodsInfoDao.countByKeyWord(parameter);
	}

	@Override
	public boolean checkGoodsNo(String goodsNo) {
		return goodsInfoDao.countByGoodsNo(goodsNo) > 0;
	}

	@Override
	public void updateByBatch(List<GoodsInfo> goodsInfos) {
		try {
			if (goodsInfoDao.updateByBatch(goodsInfos) < 0) {
				throw new ServiceException("批量更新商品信息失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("批量更新商品信息发生错误！", e);
		}
	}

	@Override
	public List<GoodsInfo> findByIds(String srcIds) {
		List<Long> ids = CommonUtil.parseToList(srcIds, ", ");
		return goodsInfoDao.findByIds(ids);
	}

}
